/*
* @Date: 2020/12/1
* @Author: XueChengwu <xuechengwu@erayt.com>
* @Copyright: 2015-2019 Erayt, Inc.
* @Description: If you have some questions, please contact: xuechengwu@erayt.com.
*/

class Decimal {
  constructor(num) {
    const numArr = `${num}`.split('.');
    this.integer = numArr[0];
    this.decimal = numArr.length > 1 ? numArr[1] : '';
  }

  compare(num) {
    
  }

  add(num) {
    const decimalLen = Math.max(num.decimal.length, this.decimal.length);
    const decimal1 = num.decimal.padEnd(decimalLen, 0);
    const decimal2 = this.decimal.padEnd(decimalLen, 0);
    let decimalFlag = 0;
    let decimalResult = ``;
    let temp = 0;
    for (let i = decimalLen - 1; i >= 0; i--) {
      temp = decimalFlag + parseInt(decimal1[i]) + parseInt(decimal2[i]);
      decimalResult = (temp % 10) + decimalResult;
      decimalFlag = parseInt(temp/10);
    }
    let integerLen = Math.max(num.integer.length, this.integer.length);
    const integer1 = num.integer.padStart(integerLen, 0);
    const integer2 = this.integer.padEnd(integerLen, 0);
    let integerFlag = 0;
    let integerResult = ``;
    for (let i = integerLen - 1; i >= 0; i--) {
      if (i === integerLen - 1) {
        temp = integerFlag + decimalFlag + parseInt(integer1[i]) + parseInt(integer2[i]);
      } else {
        temp = integerFlag + parseInt(integer1[i]) + parseInt(integer2[i]);
      }
      integerResult = (temp % 10) + integerResult;
      integerFlag = parseInt(temp / 10);
    }
    return (integerFlag === 1 ? '1' : '') + integerResult + '.' + decimalResult;
  }

  substract(num) {
    const decimalLen = Math.max(num.decimal.length, this.decimal.length);
    const decimal1 = num.decimal.padEnd(decimalLen, 0);
    const decimal2 = this.decimal.padEnd(decimalLen, 0);
    let decimalFlag = 0;
    let decimalResult = ``;
    let temp = 0;
    for (let i = decimalLen - 1; i >= 0; i--) {
      if (parseInt(decimal1[i])  - decimalFlag < parseInt(decimal2[i])) {
        decimalResult = parseInt(decimal1[i])  - decimalFlag + 10 - parseInt(decimal2[i]);
        decimalFlag = 1
      }
      temp = decimalFlag + parseInt(decimal1[i]) + parseInt(decimal2[i]);
      decimalResult = (temp % 10) + decimalResult;
      decimalFlag = parseInt(temp/10);
    }
    let integerLen = Math.max(num.integer.length, this.integer.length);
    const integer1 = num.integer.padStart(integerLen, 0);
    const integer2 = this.integer.padEnd(integerLen, 0);
    let integerFlag = 0;
    let integerResult = ``;
    for (let i = integerLen - 1; i >= 0; i--) {
      if (i === integerLen - 1) {
        temp = integerFlag + decimalFlag + parseInt(integer1[i]) + parseInt(integer2[i]);
      } else {
        temp = integerFlag + parseInt(integer1[i]) + parseInt(integer2[i]);
      }
      integerResult = (temp % 10) + integerResult;
      integerFlag = parseInt(temp / 10);
    }
    return (integerFlag === 1 ? '1' : '') + integerResult + '.' + decimalResult;
  }
}

console.log(new Decimal('99999999999999999999.9').add(new Decimal('0.1')));
